const nodemailer = require('nodemailer');


async function sendMail(config, mail, content,subject) {
    console.log('sendMail', config, mail, content)
    let transporter = nodemailer.createTransport({
        host: config.host,
        port: '465',//对应的端口号
        //secure: true,
        secureConnection: true,
        auth:{
            user: config.user,
            pass: config.pwd,
        }
    });

    let mailOptions={
        from: `"BTM 数字平台" ${config.from}`,
        to: mail,
        subject: subject,
        text: content,
    };

    return new Promise((resolve, reject)=>{
        transporter.sendMail(mailOptions,(error,info)=>{
            if(error) {
                console.error('send email error:',error.message)
                return reject(error)
            }
            console.log(`Message: ${info.messageId}`);
            console.log(`sent: ${info.response}`);
            resolve(info)
        });
    })
}

module.exports = {
    sendMail,
}
